package keye.document_manage.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import keye.document_manage.dao.sqlserver.BaseDaoSqlserver;
import keye.document_manage.dao.sqlserver.TableInterfaceListViewDao;
import keye.document_manage.domain.TableInterfaceListView;
import keye.document_manage.domain.TableInterfaceListViewKey;
import keye.document_manage.entity.Page;
import keye.document_manage.entity.SelectParameters;
import keye.document_manage.service.ITableInterfaceListViewService;
import keye.document_manage.util.MySqlProvider;

@Service("interfaceListViewService")
public class TableInterfaceListViewServiceImpl implements ITableInterfaceListViewService {

	@Resource
	private TableInterfaceListViewDao tableInterfaceListViewDao;
	@Resource
	private BaseDaoSqlserver<?> baseDao;
	
	public TableInterfaceListView getView(TableInterfaceListViewKey key) {
		return this.tableInterfaceListViewDao.selectByPrimaryKey(key);
	}

	public List<?> getViews(String tableName) {
		return baseDao.getAll(tableName);
	}

	public TableInterfaceListViewDao getTableInterfaceListViewDao() {
		return tableInterfaceListViewDao;
	}

	public void setTableInterfaceListViewDao(TableInterfaceListViewDao tableInterfaceListViewDao) {
		this.tableInterfaceListViewDao = tableInterfaceListViewDao;
	}

	public BaseDaoSqlserver<?> getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDaoSqlserver<?> baseDao) {
		this.baseDao = baseDao;
	}

	@Override
	public Map<String, Object> getList(String tableName) {
		return getList(tableName, null, null);
//		if(null == tableName || "".equals(tableName.trim()))
//			return null;
//		System.out.println("baseDao.getList("+tableName+")");
//		List<TableInterfaceListView> paras= baseDao.getParamters(tableName);
//		if(paras == null || paras.size()==0){
//			return null;
//		}
//		List<HashMap<String,Object>> rs = baseDao.getList(tableName,paras);
//		Collections.sort(paras, new Comparator<TableInterfaceListView>() {
//
//			@Override
//			public int compare(TableInterfaceListView p1, TableInterfaceListView p2) {
//				if(p1.getColumnIndex() == p2.getColumnIndex())
//					return 0;
//				if(p1.getColumnIndex() > p2.getColumnIndex())
//					return 1;
//				return -1;
//			}
//		});
//		Map<String,Object> map = null;
//		List<Object[]> datas = new ArrayList<Object[]>();
//		
//		int parasLength = paras.size();
//		String[] viewName = new String[parasLength];
//		
//		for (int j = 0; j < parasLength; j++) {
//			viewName[j] = paras.get(j).getColumnTitle();
//		}
//		Object[] objs = null;
//		for (int i = 0,length = rs.size(); i < length; i++) {
//			map = rs.get(i);
//			objs = new Object[parasLength];
//			for (int j = 0; j < parasLength; j++) {
//				objs[j] = map.get(paras.get(j).getFieldName());
//			}
//			datas.add(objs);
//		}
//		map = new HashMap<String, Object>();
//		map.put("viewName", viewName);
//		map.put("dataList", datas);
//		return map;
	}

	@Override
	public Map<String, Object> getList(String tableName, Page page) {
		return getList(tableName, null, page);
//		if(null == tableName || "".equals(tableName.trim()))
//			return null;
//		System.out.println("baseDao.getList("+tableName+")");
//		List<TableInterfaceListView> paras= baseDao.getParamters(tableName);
//		if(paras == null || paras.size()==0){
//			return null;
//		}
//		List<HashMap<String,Object>> rs = baseDao.getListByPage(tableName, paras, page);
//		Collections.sort(paras, new Comparator<TableInterfaceListView>() {
//
//			@Override
//			public int compare(TableInterfaceListView p1, TableInterfaceListView p2) {
//				if(p1.getColumnIndex() == p2.getColumnIndex())
//					return 0;
//				if(p1.getColumnIndex() > p2.getColumnIndex())
//					return 1;
//				return -1;
//			}
//		});
//		Map<String,Object> map = null;
//		List<Object[]> datas = new ArrayList<Object[]>();
//		
//		int parasLength = paras.size();
//		String[] viewName = new String[parasLength];
//		String[] fieldNames = new String[parasLength];
//		//viewName[0] = "序号";
//		for (int j = 0; j < parasLength; j++) {
//			viewName[j] = paras.get(j).getColumnTitle();
//			fieldNames[j] = paras.get(j).getFieldName();
//		}
//		Object[] objs = null;
//		for (int i = 0,length = rs.size(); i < length; i++) {
//			map = rs.get(i);
//			objs = new Object[parasLength];
//			//objs[0] = i+1;
//			for (int j = 0; j < parasLength; j++) {
//				objs[j] = map.get(paras.get(j).getFieldName());
//			}
//			datas.add(objs);
//		}
//		map = new HashMap<String, Object>();
//		map.put("fields", fieldNames);
//		map.put("viewName", viewName);
//		map.put("dataList", datas);
//		return map;
	}
	
	@Override
	public Map<String, Object> getList(String tableName, List<SelectParameters> selectParas,
			Page page) {
		if(null == tableName || "".equals(tableName.trim()))
			return null;
		List<TableInterfaceListView> paras= baseDao.getParamters(tableName);
		if(paras == null || paras.size()==0){
			return null;
		}
		Map<String,Object> map = null;
		if(selectParas != null){
			List<HashMap<String, Object>> spMap = baseDao.getFiledType(tableName, selectParas);
			
			for (int i = 0,length = spMap.size(); i < length; i++) {
				map = spMap.get(i);//t='' n='',t='' n='',t='' n=''
				SelectParameters sp = null;
				for (int j = 0; j < selectParas.size(); j++) {
					sp = selectParas.get(j);
					if(map.get(MySqlProvider.FILED_NAME).toString().equalsIgnoreCase(sp.getName())){
						sp.setType((String) map.get(MySqlProvider.FILED_TYPE));
					}
				}
//				selectParas.forEach(sp->{});
//				selectParas.stream().filter(sp->map.get(MySqlProvider.FILED_NAME).toString().equalsIgnoreCase(sp.getName()));
			}

		}
		
		List<HashMap<String,Object>> rs = baseDao.getListWithParasByPage(tableName, selectParas, paras, page);
		Collections.sort(paras, (p1, p2)-> {
				if(p1.getColumnIndex() == p2.getColumnIndex())
					return 0;
				if(p1.getColumnIndex() > p2.getColumnIndex())
					return 1;
				return -1;
		});
//		Collections.sort(paras, new Comparator<TableInterfaceListView>() {
//
//			@Override
//			public int compare(TableInterfaceListView p1, TableInterfaceListView p2) {
//				if(p1.getColumnIndex() == p2.getColumnIndex())
//					return 0;
//				if(p1.getColumnIndex() > p2.getColumnIndex())
//					return 1;
//				return -1;
//			}
//		});
		
		List<Object[]> datas = new ArrayList<Object[]>();
		int parasLength = paras.size();
		String[] fieldNames = new String[parasLength];
		String[] viewName = new String[parasLength];
//		TableInterfaceListView[] listViews = 
		for (int j = 0; j < parasLength; j++) {
			viewName[j] = paras.get(j).getColumnTitle();
			fieldNames[j] = paras.get(j).getFieldName();
		}
		Object[] objs = null;
		
		for (int i = 0,length = rs.size(); i < length; i++) {
			map = rs.get(i);
			objs = new Object[parasLength];
			for (int j = 0; j < parasLength; j++) {
				objs[j] = map.get(paras.get(j).getFieldName());
			}
			datas.add(objs);
		}
		map = new HashMap<String, Object>();
		map.put("fields", fieldNames);
		map.put("viewName", viewName);
		map.put("dataList", datas);
		map.put("listViews", paras);
		return map;
	}


	@Override
	public Map<String, Object> getList(String tableName,
			List<SelectParameters> selectParas, Page page, String where) {
		if(null == tableName || "".equals(tableName.trim()))
			return null;
		List<TableInterfaceListView> paras= baseDao.getParamters(tableName);
		if(paras == null || paras.size()==0){
			return null;
		}
		Map<String,Object> map = null;
		if(selectParas != null){
			List<HashMap<String, Object>> spMap = baseDao.getFiledType(tableName, selectParas);
			for (int i = 0,length = spMap.size(); i < length; i++) {
				map = spMap.get(i);//t='' n='',t='' n='',t='' n=''
				SelectParameters sp = null;
				for (int j = 0; j < selectParas.size(); j++) {
					sp = selectParas.get(j);
					if(map.get(MySqlProvider.FILED_NAME).toString().equalsIgnoreCase(sp.getName())){
						sp.setType((String) map.get(MySqlProvider.FILED_TYPE));
					}
				}
			}
		}
		
		List<HashMap<String,Object>> rs = baseDao.getListWithDiyParasByPage(tableName, selectParas, paras, page,where);
		
		Collections.sort(paras, (p1, p2)-> {
				if(p1.getColumnIndex() == p2.getColumnIndex())
					return 0;
				if(p1.getColumnIndex() > p2.getColumnIndex())
					return 1;
				return -1;
		});
		
		List<Object[]> datas = new ArrayList<Object[]>();
		int parasLength = paras.size();
		String[] fieldNames = new String[parasLength];
		String[] viewName = new String[parasLength];
		for (int j = 0; j < parasLength; j++) {
			viewName[j] = paras.get(j).getColumnTitle();
			fieldNames[j] = paras.get(j).getFieldName();
		}
		Object[] objs = null;
		
		for (int i = 0,length = rs.size(); i < length; i++) {
			map = rs.get(i);
			objs = new Object[parasLength];
			for (int j = 0; j < parasLength; j++) {
				objs[j] = map.get(paras.get(j).getFieldName());
			}
			datas.add(objs);
		}
		map = new HashMap<String, Object>();
		map.put("fields", fieldNames);
		map.put("viewName", viewName);
		map.put("dataList", datas);
		map.put("listViews", paras);
		return map;
	}
	
}
